package com.example.taobaou.ui.activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.MenuItem;

import com.example.taobaou.R;
import com.example.taobaou.base.BaseActivity;
import com.example.taobaou.base.BaseFragment;
import com.example.taobaou.ui.fragment.HomeFragment;
import com.example.taobaou.ui.fragment.OnSellFragment;
import com.example.taobaou.ui.fragment.SearchFragment;
import com.example.taobaou.ui.fragment.SelectedFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity implements IMainActivity{

    private BottomNavigationView bottomNavigationView;
    private static final String TAG = "MainActivity";
    private HomeFragment homeFragment;
    private OnSellFragment onSellFragment;
    private SearchFragment searchFragment;
    private SelectedFragment selectedFragment;
    private FragmentManager fm;

    @Override
    protected void initPresenter() {

    }

    @Override
    protected int getResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEvent() {
        initListener();
    }

    @Override
    protected void initView() {
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        initFragment();
    }

    private void initListener() {
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
                    Log.d(TAG, "首页 ");
                    switchFragment(homeFragment);
                } else if (item.getItemId() == R.id.selected) {
                    Log.d(TAG, "精选 ");
                    switchFragment(selectedFragment);
                } else if (item.getItemId() == R.id.red_packet) {
                    Log.d(TAG, "特惠 ");
                    switchFragment(onSellFragment);
                } else if (item.getItemId() == R.id.searh) {
                    Log.d(TAG, "搜索 ");
                    switchFragment(searchFragment);
                }
                return true;
            }
        });
    }

    public void goSearch(){
        bottomNavigationView.setSelectedItemId(R.id.searh);
    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        onSellFragment = new OnSellFragment();
        searchFragment = new SearchFragment();
        selectedFragment = new SelectedFragment();
        fm = getSupportFragmentManager();
        switchFragment(homeFragment);
    }

    private BaseFragment lastOneFragment = null;//上次加载的页面

    public void switchFragment(BaseFragment fragment) {
        //如果上一页等于现在页直接跳过
        if (lastOneFragment == fragment){
            return;
        }

        FragmentTransaction transaction = fm.beginTransaction();
        //该方法会加载所有页面并保存，每次切换是隐藏上一个并显示新加载的
        if (!fragment.isAdded()) {
            //如果该页面没有被添加过，添加进去
            transaction.add(R.id.main_page_container, fragment);
        } else {
            //如果被添加过，则是把他show出来
            transaction.show(fragment);
        }
        if (lastOneFragment != null) {
            //并隐藏上一个显示的页面
            transaction.hide(lastOneFragment);
        }
        //最后把现在正显示的页面置为上一页
        lastOneFragment = fragment;

        //该方法会每次加载新的页面，之前的页面会被销毁
        // transaction.replace(R.id.main_page_container,fragment);
        transaction.commit();
    }

}
